<?php
namespace Home\Controller;
use Common\Controller\HomebaseController;
class MessageController extends  HomebaseController {
    public function index(){
        $articleModel = new \Common\Model\ArticleModel();
        $where = array('is_recycle'=>0,'status'=>1);
        $order = " is_top DESC,add_time DESC , aid DESC";
        $setConfig = array(
            'next' => 'Next →',
            'prev' => '← Previous',
        );
        $data = $articleModel->getListData(4,$where,$order,$setConfig);
		//获取内容的图片
		for($i=0;$i<count($data['list']);$i++)
		{
			//echo '<pre>';
			//var_dump($data['list']);
			
			//定义获取内容中图片路径的变量
				$str = $data['list'][$i]['content'];
				$str = strip_tags($str, '<img>');
				preg_match_all('/\<img\s+src\=\"([\w:\/\.]+)\"/', $str, $matches);
				//var_dump($matches[1]);
				$match = $matches[1];
				$data['list'][$i]['contentImgs'] = $match;
		}
        $this->assign('data',$data);
        $this->assign('cocolait','');
        $this->display();
    }

	public function ajaxMessageGetMore(){
        $page = $_GET['p'];
			$articleModel = new \Common\Model\ArticleModel();
			$where = array('is_recycle'=>0,'status'=>1);
			$order = " is_top DESC,add_time DESC , aid DESC";
			//$Page = new \Think\Page(4,$page);
			
			$shu = 4*($page-1);
			$datas = $articleModel->query("select * from cp_article");
			$total = count($datas);
			$data = $articleModel->query("select * from cp_article order by add_time desc   limit $shu, 4 ");
		//获取内容的图片
		for($i=0;$i<count($data);$i++)
		{
			//echo '<pre>';
			//var_dump($data['list']);
			
			//定义获取内容中图片路径的变量
				$str = $data[$i]['content'];
				$str = strip_tags($str, '<img>');
				preg_match_all('/\<img\s+src\=\"([\w:\/\.]+)\"/', $str, $matches);
				//var_dump($matches[1]);
				$match = $matches[1];
				$data[$i]['contentImgs'] = $match;
		}
        $this->assign('data',$data);
        $this->assign('cocolait','');
        $this->display();
    }


    public function cheat_save(){
        $data = array();
        $data['title'] = trim(I('post.title',''));
        $data['url']   = trim(I('post.url',''));
        $data['create_at'] = date("Y-m-d H:i:s");
        $data['status']=1;
        
        if(M('cheat')->add($data)){
            die(json_encode(['code'=>'ok','msg'=>'添加成功']));
        }else{
            die(json_encode(['code'=>'error','msg'=>'添加失败！']));
        }
    }
}
